// pages/good-payment/good-payment.js
const util = require('../../utils/util.js');
import api from '../../utils/request/api'
import {
	orderDetail,
	addressList,
	pay,
	userCoupon,userIntegral
} from '../../utils/request/apiurl'


Page({
	data: {
		// goodList: [{
		// 	id: 1,
		// 	img: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
		// 	title: '蒙牛  纯甄 常温酸牛奶 青柠抹茶牛油果口味200g*16礼盒装',
		// 	oldPrice: 68.00,
		// 	newPrice: 36.60,
		// 	sales: 32515,
		// 	discount: 7.8,
		// 	goodNum:3,
		// }],
		goodList: [],
		id: '', //购物车id
		addressId: '', //收货地址id
		total: 0, //总价格
		adress: '',
		uid: '',
		isPay: true, //支付方式弹窗
		isDelivery: true, //提货方式弹窗
		tickets: true, //优惠券弹窗
    payIs:false,//默认显示立即支付按钮
		payType: 1, //支付方式
		pickUp: 0, //提货方式
		orderComment: '', //备注
		payment: [{
				id: 0,
				name: '余额支付',
				value: '余额支付',
				
			},
			{
				id: 1,
				name: '微信支付',
				value: '微信支付',
        checked: 'true'
			},
			// {
			// 	id: 2,
			// 	name: '积分兑换',
			// 	value: '积分兑换'
			// },
		],
		delivery: [{
				id: 0,
				name: '网点自提',
				value: '网点自提',
				checked: 'true'
			},
			{
				id: 1,
				name: '自助售贩机提取',
				value: '自助售贩机提取'
			},
		],
		topTab: [{
				title: "可使用",
				action: true
			},
			// {
			//   title: "不可使用",
			//   action: false
			// },
			// {
			//   title: "已过期",
			//   action: false
			// }
		],
		couponList: [],
		searchGood: {
			page: 1,
			size: 10,
			pages: 0
		},
		discountId: '', //优惠券id
		Index: 0,
		orderNo: '', //订单号
		openid: '',
		showPayPwdInput: false, //是否展示密码输入层
		pwdVal: '', //输入的密码
		payFocus: true, //文本框焦点
		isPayPassword:'',//是否绑定支付密码
		pickUpAll:'',//提货
		isRequest: false,
		totalMoneyAll:0,
    shopOriginalPrice:0,//下单计价传原价+配送费
		type:'',
		integrals:true,//积分兑换弹窗
		// totalIntegrals:0,//商品兑换所需积分
		Integrals:0,//选择积分兑换
		myIntegrals:0,//我的积分
		maximum:0,//兑换所需积分
		scale:0,//兑换比例
		isNewOpen:false,//是否是返回页
		IntegrationRule:'',//积分规则
    sellCountPrice:0,//会员对应折扣价格
    distributionFee: 0,//配送费
    distributionFeeTotal: 0,//总价格不包括配送费
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		// console.log(options)
		// console.log(wx.getStorageSync('orderComment'))
		var that=this
		var userInfo = wx.getStorageSync('userInfo');
		this.setData({
			uid: userInfo.uid,
			openid: userInfo.xcxOpenid,
			id: options.id,
		})
		if (options.orderNo) {
			this.setData({
				orderNo: options.orderNo,
				// total: options.totalMoney,
				id: options.id,
				pickUp: options.pickUp,
			})
		}
		this.setData({
			id: options.id.split(','),
			// total:options.total
		})
		// this.orderDetailList();
		this.addressList();
		if(options.type=='1'){
			// debugger
			// console.log(wx.getStorageSync('discountPrice'))
			var userI = wx.getStorageSync('orderComment');
			this.setData({
				'couponList[this.data.Index].discountPrice':wx.getStorageSync('discountPrice'),
				orderComment:userI,
				type:1,
			})
			api.post(addressList.addressDetail,{
				id:this.data.adressid
			}).then(res => {
				// console.log(res)
				// if()
				this.setData({
					adress:res
				})
			}).catch(err => {
				// console.log(err)
				wx.showToast({
					title: err.message,
					icon: 'none'
				})
			})
		}
		// this.showInputLayer()

	},
	//显示支付密码输入层
	showInputLayer: function() {
		this.setData({
			showPayPwdInput: true,
			payFocus: true,
			pwdVal:''
		});
	},
	// /**
	//  * 隐藏支付密码输入层
	//  */
	hidePayLayer: function() {
		var val = this.data.pwdVal;
		this.setData({
			showPayPwdInput: false,
			payFocus: false,
			pwdVal: ''
		});
		wx.showModal({
		  title: '取消支付',
		  icon: 'none',
		  success (res) {
			setTimeout(function() {
			  wx.redirectTo({
			  	url: '../myOrder/myOrder?type=1'
			  })
			}, 1000);
		  }
		})
	},
	// 我的积分
	userIntegral(){
		var source = {
			uid: this.data.uid,
		}
		api.post(userIntegral.integral, source).then(res => {
			// console.log(res)
			this.setData({
				myIntegrals:res
			})
			
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
		
	},
	
	
	// 忘记密码
	forgetPassWord(){
		// var val = this.data.pwdVal;
		wx.showModal({
		  title: '忘记密码',
		  content: '是否前往更换密码',
		  success (res) {
		    if (res.confirm) {
				wx.navigateTo({
					url: '../personalInfo/personalInfo'
				})
		      // url: '../personalInfo/personalInfo'
		    } else if (res.cancel) {
				  this.setData({
					showPayPwdInput: false,
					payFocus: false,
					pwdVal: ''
				  });
		    }
		  }
		})
		// wx.redirectTo({
		// 	url: '../myOrder/myOrder?type=1'
		// })
	},
	
	//确定支付(余额支付)
	surePay(){
		this.setData({
			pwdVal:this.data.pwdVal,
			// showPayPwdInput: false,
		})
		if(this.data.pwdVal.length==6){
			// console.log(this.data.Integrals,'this.data.Integralsthis.data.Integrals')
			if(this.data.Integrals==0){
				if (this.data.discountId != null && this.data.discountId != undefined && this.data.discountId != "") {
					var source = {
						totalMoney:parseFloat(this.data.total),
						orderNo: this.data.orderNo,
						openId: this.data.openid,
						discountId: this.data.discountId, //优惠券id
						// password: 123457, //支付密码
						password:this.data.pwdVal,
					}
				} else {
					var source = {
						totalMoney: parseFloat(this.data.total),
						orderNo: this.data.orderNo,
						openId: this.data.openid,
						// discountId:this.data.discountId,//优惠券id
						// password: 123457, //支付密码
						password:this.data.pwdVal,
					}
				}
			}else{
				var source = {
					totalMoney: parseFloat(this.data.total),
					orderNo: this.data.orderNo,
					openId: this.data.openid,
					// discountId:this.data.discountId,//优惠券id
					// password: 123457, //支付密码
					password:this.data.pwdVal,
					integral:this.data.Integrals,//积分
				}
			}
			// console.log(source)
			api.post(pay.BalancePay, source).then(res => {
				// console.log(res)
				var orderId = res.split(':')
				if(orderId[1] == 'OK'){
          //判断用户是否需要重新加载商品列表页
          wx.setStorageSync('isLogin', true)
					wx.showToast({
					    title: '支付成功',
					    icon: 'none',
					    success: function () {
					      setTimeout(function() {
					        wx.redirectTo({
					        	url: '../myOrder/myOrder?type=2'
					        })
					      }, 1000);
					    }
					});
					
				}
			}).catch(err => {
				// console.log(err)
				// wx.showToast({
				// 	title: err.message,
				// 	icon: 'none'
				// })
				// wx.redirectTo({
				// 	url: '../shopping-cart/shopping-cart'
				// })
				this.setData({
					showPayPwdInput: true,
					pwdVal:'',
				})
        wx.redirectTo({
          url: '../shopping-cart/shopping-cart'
        })
				wx.showToast({
				    title: err.message,
				    icon: 'none',
				    success: function () {
				      setTimeout(function() {
                console.log(err.message)
				        // wx.redirectTo({
				        // 	url: '../shopping-cart/shopping-cart'
				        // })
                wx.redirectTo({
                  url: '../myOrder/myOrder?type=1'
                })
				      }, 1000);
				    }
				});
			})
		}	
	},
	/**
	 * 获取焦点
	 */
	getFocus: function(e) {
		// console.log(e)
		this.setData({
			payFocus: true
		});
	},
	/**
	 * 输入密码监听
	 */
	inputPwd: function(e) {
   
		this.setData({
			pwdVal: e.detail.value
		});

    if (this.data.pwdVal.length==6){
      this.surePay()
    }
		// console.log(this.data.pwdVal,'pwd')
	},
	// 获取优惠券
	userCouponList(type) {
		let nowType = 0
		if (type) {
			nowType = type
		}
		var source = {
			page: this.data.searchGood.page,
			size: this.data.searchGood.size,
			uid: this.data.uid,
			type: 0
		}
		api.post(userCoupon.list, source).then(res => {
      // debugger
			let list = res.rows
			var dataList=[]
			// debugger
			list.map((a,i) => {
				a.time = util.getLocalTime(a.outTime)
				// console.log(a)
				if (nowType === 0) {
					//未使用
					let dayTime = (a.outTime * 1000) - new Date().getTime()
					let dayLine = 3 * 24 * 60 * 60 * 1000
					if (dayTime < dayLine) {
						a.toExpire = true
					}
					// console.log(a.discountMinPrice,this.data.total)
					if (a.discountMinPrice <= this.data.total) {
						// console.log(a.discountMinPrice,this.data.total)
						dataList.push(a)
          }
          // console.log(dataList)
          this.setData({
            couponList: dataList,
          })
				}
			})
			var pages=res.total/this.data.searchGood.size
			var totaMoney=0;
			// debugger
			// console.log(dataList.length)
      //得到会员折扣的价格缓存
      // shopOriginalPrice//下单支付时money传原价
      var rankSellcount = wx.getStorageSync('userInfo');
			if(dataList.length>0){
				// console.log(this.data.goodList)
				// console.log(dataList)
				// console.log(this.data.Integrals)
				if(this.data.IntegrationRule!=null&&this.data.Integrals!=0){
					// 可以积分兑换 （(this.data.total/100)-(dataList[this.data.Index].discountPrice/100))<0?0:(this.data.total/100)-(dataList[this.data.Index].discountPrice/100
          totaMoney = parseFloat(new Number((((this.data.distributionFeeTotal / 100) * (rankSellcount.sellCount / 100)) - (this.data.Integrals) / (this.data.IntegrationRule.scale) - (dataList[this.data.Index].discountPrice / 100)) < 0 ? 0 : (((this.data.distributionFeeTotal / 100) * (rankSellcount.sellCount / 100)) -(this.data.Integrals)/(this.data.IntegrationRule.scale)-(dataList[this.data.Index].discountPrice/100))).toFixed(2))
					// console.log(totaMoney)
				}else{					
          totaMoney = parseFloat(new Number((((this.data.distributionFeeTotal / 100) * (rankSellcount.sellCount / 100)) - (dataList[this.data.Index].discountPrice / 100)) < 0 ? 0 : ((this.data.distributionFeeTotal / 100) * (rankSellcount.sellCount / 100))-(dataList[this.data.Index].discountPrice/100)).toFixed(2))
					// console.log(totaMoney)
				}	
			}else{
				if(this.data.IntegrationRule!=null&&this.data.Integrals!=0){
					// debugger
          totaMoney = parseFloat(new Number(((this.data.distributionFeeTotal / 100) * (rankSellcount.sellCount / 100))-(this.data.Integrals)/(this.data.IntegrationRule.scale)).toFixed(2)) 
					// console.log(totaMoney)
				}else{					
          totaMoney = parseFloat(new Number(((this.data.distributionFeeTotal / 100) * (rankSellcount.sellCount / 100))).toFixed(2))
					// console.log(totaMoney)
				}
			}
			// console.log(this.data.Integrals,'this.data.Integrals')
			// console.log(this.data.goodList[0].scale,'this.data.goodList[0].scale')
			// console.log(totaMoney)
      if (totaMoney + (this.data.distributionFee / 100) <0){
				this.setData({
					totalMoneyAll:0
				})
			}else{
        if (rankSellcount.sellCount){
          // + (this.data.distributionFee / 100)
          this.setData({
            // totalMoneyAll: totaMoney * (rankSellcount.sellCount / 100),
            totalMoneyAll: totaMoney + (this.data.distributionFee / 100),
            shopOriginalPrice: totaMoney + (this.data.distributionFee / 100), 
          })
        }else{
          // + (this.data.distributionFee / 100)
          this.setData({
            totalMoneyAll: totaMoney + (this.data.distributionFee / 100),
            shopOriginalPrice: totaMoney + (this.data.distributionFee / 100), 
          })
        }
			}
			this.setData({
			  searchGood: {
			    page: this.data.searchGood.page,
			    size: this.data.searchGood.size,
			    pages: pages,				
			  },
			})
			if(this.data.couponList.length>0){
				this.setData({
					discountId:this.data.couponList[this.data.Index].id
				})
			}
      console.log("优惠券数据",this.data.couponList)
		}).catch(err => {
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	},

	onShow: function() {
		var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo,'userInfouserInfouserInfo')
		this.setData({
			uid: userInfo.uid,
			openid: userInfo.xcxOpenid,
			isPayPassword:userInfo.isPayPassword,
      sellCountPrice:userInfo.sellCount
		})		
		var pages = getCurrentPages(); // 获取页面		 
		var currPage = pages[pages.length - 1]; // 当前页面		 
		var prevPage = pages[pages.length - 2]; // 上一个页面	
		this.orderDetailList();
		if(this.data.isNewOpen){
			// debugger
			this.setData({
				adress:this.data.adress,
				addressId:this.data.adress.id ,
			})
		}	
		// this.addressList();
		 // console.log(prevPage.data,'prevPage.data.addressInfor')
		 
		// this.setData({
		// 	adress:prevPage.data.addressInfor
		// })	
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		wx.showNavigationBarLoading() //在标题栏中显示加载
		this.setData({
			couponList:[],
		})
		this.orderDetailList();
		// this.addressList();
		setTimeout(function() {
			wx.hideNavigationBarLoading() //完成停止加载
			wx.stopPullDownRefresh() //停止下拉刷新
		}, 1500);
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
			this.setData({
			  searchGood: {
			    page: this.data.searchGood.page + 1,
			    size: 10,
			    pages: this.data.searchGood.pages
			  }
			})
			this.userCouponList()
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	// 积分规则
	integralMore(){
		var source = {
			money: this.data.total,
		}
		api.post(orderDetail.integralMore, source).then(res => {
			this.setData({
				IntegrationRule:res
			})
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	},
	// 商品详情
	orderDetailList() {
		// console.log(this.data.id)
		var orderList = [];
		// debugger
		// if (this.data.id instanceof Array) {
		for (var i = 0; i < this.data.id.length; i++) {
			// console.log(this.data.id[i]+',')
			var source = {
				id: this.data.id[i]
			}
		api.post(orderDetail.Detail, source).then(res => {
			var  skuList=[]
			var goodsSku=JSON.parse(res.sku)
			var skudata=''
			for(var obj in goodsSku){
				if(skudata==''){
					skudata=goodsSku[obj]
				}else{
					skudata=skudata+'/'+goodsSku[obj]
				}
			}
			skuList.push(skudata)
			res['skuList']=skuList
			orderList.push(res)
			// console.log(orderList)
			this.setData({
				goodList: orderList,
				pickUpAll:res.pickUpAll,	
			})
			// console.log(this.data.pickUpAll)
			
			// if(this.data.pickUpAll==0){
			// 	this.setData({
			// 		delivery:this.data.delivery
			// 	})	
			// }else if(this.data.pickUpAll==1){
			// 	var delivery=[{
			// 			id: 0,
			// 			name: '网点自提',
			// 			value: '网点自提',
			// 			checked: 'true'
			// 		}]
			// 	this.setData({
			// 		delivery:delivery
			// 	})
			// }else if(this.data.pickUpAll==2){
			// 	var delivery=[{
			// 			id: 1,
			// 			name: '自助售贩机提取',
			// 			value: '自助售贩机提取',
			// 			checked: 'true'
			// 		}]
			// 	this.setData({
			// 		delivery:delivery
			// 	})
			// }
			var goodList = this.data.goodList
			var total = 0
			var all = 0
			var deliveryPrice = 0;//运费
			// var totalIntegrals=0;
			// debugger
			for (var i = 0; i < goodList.length; i++) {
        // var price = parseFloat(goodList[i].currentPrice * goodList[i].goodsCount)
        var price = parseFloat(goodList[i].goodsPrice * goodList[i].goodsCount)
				all +=price ;
				if(goodList[i].deliveryPrice!=''&&goodList[i].deliveryPrice!=null&&goodList[i].deliveryPrice!=undefined){
					var delivery=parseFloat(goodList[i].deliveryPrice)
					deliveryPrice +=delivery ;
				}
        //折扣计算不需要运费计算
        total = all + deliveryPrice;
        // total = all;
				this.setData({
					total: total,
          distributionFeeTotal:all,//总价格不包括配送费
          distributionFee: deliveryPrice,//赋值配送费
				})
				// console.log(all)
				// console.log(deliveryPrice)
				// 商品兑换所需积分
				// totalIntegrals=goodList[i].scale*(goodList[i].goodsPrice /100 * goodList[i].goodsCount)		
				// maximum	=goodList[i].maximum
			}
			// this.setData({
			// 	total: total,
			// 	// maximum:maximum,
			// })
			this.integralMore();
			this.userCouponList();
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
		}
	},
	// 地址列表
	addressList() {
			var source = {
				uid: this.data.uid,
			}
			api.post(addressList.list, source).then(res => {
				// console.log(res)
				for (var i = 0; i < res.length; i++) {
					if (res[i].isDefault == 1) {
						this.setData({
							adress: res[i],
							addressId: res[i].id,
						})
					}
				}
			}).catch(err => {
				// console.log(err)
				wx.showToast({
					title: err.message,
					icon: 'none'
				})
			})
		
	},
	inputorderComment(e){
		// console.log(e.detail.value)
		this.setData({
			orderComment:e.detail.value
		})
	},
	// 地址详情
	adressDetail() {
		// console.log(this.data.orderComment)
		if(this.data.orderComment!=''){
			wx.setStorageSync('orderComment', this.data.orderComment);
		}else{
			wx.setStorageSync('orderComment', '');
		}
		
		if(this.data.couponList!=null&&this.data.couponList!=undefined&&this.data.couponList!=''&&this.data.couponList[this.data.Index].discountPrice!=''){
			wx.setStorageSync('discountPrice', this.data.couponList[this.data.Index].discountPrice);
		}else{
			wx.setStorageSync('discountPrice', '');
		}
		this.setData({
			isNewOpen:true
		})
		wx.navigateTo({
			url: '../receiveing-adress/receiveing-adress?id=' + this.data.id
		})
	},
	//选择支付方式
	paymentList() {
		this.setData({
			isPay: false,
      payIs:true,
		})
	},
	//提货方式
	// deliveryDetail() {
	// 	this.setData({
	// 		isDelivery: false,
	// 		
	// 	})
	// },
	radioChange(e) {
		this.setData({
			payType: e.detail.value
		})
	},
	radioChangeDelivery(e) {
		// console.log(e)
		this.setData({
			pickUp: e.detail.value
		})
	},
	paySure() {
		this.setData({
			isPay: true,
      payIs:false,
		})
	},
	deliverySure() {
		this.setData({
			isDelivery: true
		})
	},
	// 优惠券弹窗
	tickets() {
		this.setData({
			tickets: false,
      payIs: true,//隐藏
		})
	},
	// 积分兑换弹窗
	integral(){
		this.userIntegral()
		this.setData({
			integrals:false,
      payIs:true,
		})
	},
	// 积分兑换取消
	IntegralsCancel(){
		this.setData({
			integrals:true,
			Integrals:0
		})
	},
	Integrals(e){
		// console.log(e)
		this.setData({
			Integrals:e.detail.value,
		})			
	},
	IntegralsSure(){
		// debugger
		
		if(this.data.couponList.length>0){
			var discountPrice=(this.data.couponList[this.data.Index].discountPrice)//优惠券价格
			// console.log(this.data.total,discountPrice,this.data.Integrals,this.data.IntegrationRule.scale)
      if (this.data.Integrals < this.data.IntegrationRule.scale*10){
          wx.showToast({
            title: '至少需要' + this.data.IntegrationRule.scale * 10 +'积分才能兑换',
            icon: 'none'
          })
          return false
			}else if(this.data.Integrals>this.data.myIntegrals){
				wx.showToast({
					title: '您的积分不足,请重新输入',
					icon: 'none'
				})
				return false
			}else if(this.data.Integrals>this.data.IntegrationRule.maximum){
				// console.log(this.data.maximum)
				wx.showToast({
					title: '超出了商品可兑换的最大值，请重新输入',
					icon: 'none'
				})
				return false
			}else if(this.data.couponList.length>0&&this.data.Integrals/this.data.IntegrationRule.scale>(this.data.total-discountPrice)/100){
					wx.showToast({
						title: '超了，请重新输入',
						icon: 'none'
					})
					return false
			}else{
				this.setData({
					integrals:true,
          payIs:false,//显示
				})
				this.orderDetailList()
			}
		}else{
			// console.log(this.data.total,discountPrice,this.data.Integrals,this.data.IntegrationRule.scale)
      if (this.data.Integrals < this.data.IntegrationRule.scale * 10) {
        wx.showToast({
          title: '至少需要' + this.data.IntegrationRule.scale * 10 + '积分才能兑换',
          icon: 'none'
        })
        return false
      } else if(this.data.Integrals>this.data.myIntegrals){
				wx.showToast({
					title: '您的积分不足,请重新输入',
					icon: 'none'
				})
				return false
			}else if(this.data.Integrals>this.data.IntegrationRule.maximum){
				// console.log(this.data.maximum)
				wx.showToast({
					title: '超出了商品可兑换的最大值，请重新输入',
					icon: 'none'
				})
				return false
			}else if(this.data.Integrals/this.data.IntegrationRule.scale>(this.data.total)/100){
					wx.showToast({
						title: '超了，请重新输入',
						icon: 'none'
					})
					return false
			}else{
				this.setData({
          integrals: true,
          payIs: false,//显示
				})
				this.orderDetailList()
			}
		}
		
		
		
	},
	
	// 选择优惠券
	ticketSure(e) {
		var totaMoney=0
		// console.log(this.data.Integrals)
		// debugger
		if(this.data.couponList.length>0 ){
			var a=new Number((this.data.total/100)-(this.data.couponList[e.currentTarget.dataset.index].discountPrice/100)-(this.data.Integrals)/(this.data.IntegrationRule.scale)).toFixed(2)
			totaMoney=parseFloat(a)<0?0:parseFloat(a)
			// console.log(a,totaMoney)
			// totaMoney=parseFloat(new Number((this.data.total/100)-(this.data.couponList[e.currentTarget.dataset.index].discountPrice/100)-(this.data.Integrals)/(this.data.IntegrationRule.scale)).toFixed(2)<0?0:parseFloat(new Number((this.data.total/100)-(this.data.couponList[e.currentTarget.dataset.index].discountPrice/100)-(this.data.Integrals)/(this.data.IntegrationRule.scale))).toFixed(2)
		}else{
			var b=new Number(this.data.total/100-(this.data.Integrals)/(this.data.IntegrationRule.scale)).toFixed(2)
			totaMoney=parseFloat(b)
			// console.log(b,totaMoney)
			// totaMoney=parseFloat(new Number(this.data.total/100-(this.data.Integrals)/(this.data.IntegrationRule.scale))).toFixed(2)
		}
		this.setData({
			Index: e.currentTarget.dataset.index,
			discountId: e.currentTarget.dataset.id,
			tickets: true,
      payIs:false,//显示
      totalMoneyAll: totaMoney ,
      shopOriginalPrice: totaMoney + (this.data.distributionFee / 100), 
    })
    this.orderDetailList();//重加载
	},

	// 支付
	payment(formid) {
		// debugger
		console.log(this.data.addressId)
		// debugger
		// this.adressVery()
    if (this.data.addressId == "" || this.data.addressId == undefined){
			 return wx.showToast({
				title: '收货地址不能为空',
				icon: 'none'
			})
		}
		// debugger
		// console.log(this.data.totalMoneyAll)
		if(this.data.Integrals==0){
			if (this.data.discountId != null && this.data.discountId != undefined && this.data.discountId != '') {
				var source = {
					uid: this.data.uid,
					payType: this.data.payType,
					orderComment: this.data.orderComment,
					takeAddressId: this.data.addressId,
					// pickUp: this.data.pickUp,
					pickUp:0,
					cartId: this.data.id,
					orderType: 2,
					// integralId:this.data.IntegrationRule.id,
					discountId: this.data.discountId, //优惠券id
          money: parseInt(this.data.total), 
          // money: this.data.totalMoneyAll.toFixed(2) * 100,
          // money: this.data.shopOriginalPrice.toFixed(2) * 1000 / 10, 
					formId:formid
				}
			} else {
				var source = {
					uid: this.data.uid,
					payType: this.data.payType,
					orderComment: this.data.orderComment,
					takeAddressId: this.data.addressId,
					// pickUp: this.data.pickUp,
					pickUp:0,
					cartId: this.data.id,
					orderType: 2,
					// integralId:this.data.IntegrationRule.id,
          money: parseInt(this.data.total),
          // money: this.data.totalMoneyAll.toFixed(2) * 100,
          // money: this.data.shopOriginalPrice.toFixed(2) * 1000 / 10, 
					formId:formid,
					// money:1
					// discountId: this.data.discountId, //优惠券id
				}
			}
		}else{
			if (this.data.discountId != null && this.data.discountId != undefined && this.data.discountId != '') {
				var source = {
					uid: this.data.uid,
					payType: this.data.payType,
					orderComment: this.data.orderComment,
					takeAddressId: this.data.addressId,
					// pickUp: this.data.pickUp,
					pickUp:0,
					cartId: this.data.id,
					integralId:this.data.IntegrationRule.id,
					orderType: 2,
					money:parseFloat(this.data.total),
          // money: this.data.totalMoneyAll.toFixed(2) * 100,
          // money: this.data.shopOriginalPrice.toFixed(2) * 1000 / 10, 
					integral:this.data.Integrals,//积分
					formId:formid,
					discountId: this.data.discountId, //优惠券id
				}
			}else{
				var source = {
					uid: this.data.uid,
					payType: this.data.payType,
					orderComment: this.data.orderComment,
					takeAddressId: this.data.addressId,
					// pickUp: this.data.pickUp,
					pickUp:0,
					cartId: this.data.id,
					integralId:this.data.IntegrationRule.id,
					orderType: 2,
          money: parseFloat(this.data.total),
          // money: this.data.totalMoneyAll.toFixed(2) * 100,
          // money: this.data.shopOriginalPrice.toFixed(2) * 1000 / 10, 
					integral:this.data.Integrals,//积分
					formId:formid
				}
			}
			
		}
    console.log("立即支付", source, this.data.shopOriginalPrice.toFixed(2) * 1000 / 10)
		// console.log(source,'sourcesource')
		api.post(orderDetail.createOrder, source).then(res => {
			// console.log(res)
			// debugger
			if (res != '' || res != undefined || res != null) {
				// console.log(this.data.openid)
				this.setData({
					orderNo: res
				})
				if (this.data.payType == 1) {
					if (this.data.discountId != null && this.data.discountId != undefined && this.data.discountId != '') {
						var source = {
							totalMoney:parseInt(this.data.total) ,
							// totalMoney: 1,
							orderNo: this.data.orderNo,
							openId: this.data.openid,
							discountId: this.data.discountId, //优惠券id
						}
					} else {
						var source = {
							totalMoney:parseInt(this.data.total) ,
							// totalMoney: 1,
							orderNo: this.data.orderNo,
							openId: this.data.openid,
							// discountId:this.data.discountId,//优惠券id
						}
					}
					// console.log(source)
					api.post(pay.WxPay, source).then(res => {
						// console.log(res, 444)
						if (res.payMessage.result_code == 'SUCCESS') {
							// debugger
							// for (var i = 0; i < res.length; i++) {
							// 	if (res[i].isDefault == 1) {
							// 		this.setData({
							// 			adress: res[i],
							// 			addressId: res[i].id,
							// 		})
							// 	}
							// }
							wx.requestPayment({
								'timeStamp': res.payMessage.time, //时间戳
								'nonceStr': res.payMessage.nonce_str, //随机串
								'package': res.payMessage.prepay_id, //数据包
								'signType': res.payMessage.signType, //签名方式
								'paySign': res.payMessage.sign,
								// 'notify_url':'http://backstage.dfbs-vm.com:443/boda/order/notify',
								// 'notify_url':'http://26j7g84917.qicp.vip/boda/order/notify',
								'notify_url':'http://backstage.dfbs-vm.com:8112/boda/order/notify',
								success(res) {

                  //缓存判断用户是否需要重新加载商品列表页
                  wx.setStorageSync('isLogin', true)
									// console.log(res, 111)
									if (res.errMsg == 'requestPayment:ok') {
										wx.showToast({
											title: '支付成功',
											icon: 'none'
										})
										wx.redirectTo({
											url: '../myOrder/myOrder?type=2'
										})
									}
								},
								fail(res) {
									// console.log(res)
									wx.showToast({
									    title: '支付失败',
									    icon: 'none',
									    success: function () {
									      setTimeout(function() {
									        wx.redirectTo({
									        	url: '../myOrder/myOrder?type=1'
									        })
									      }, 1000);
									    }
									});
									
								}
							})
						}
					}).catch(err => {
						// console.log(err)
						wx.showToast({
						    title: err.message,
						    icon: 'none',
						    success: function () {
						      setTimeout(function() {
						        wx.switchTab({
						        	url: '../shopping-cart/shopping-cart'
						        })
						      }, 1000);
						    }
						});						
					})
				} else if (this.data.payType == 0) {
          console.log(this.data.isPayPassword,'this.data.isPayPasswordthis.data.isPayPassword')
					if(this.data.isPayPassword==0){
						wx.showToast({
						    title: '请前往绑定支付密码',
						    icon: 'none',
						    success: function () {
						      setTimeout(function() {
						        wx.navigateTo({
						        	// url: '../personalInfo/personalInfo'
                      url: '../comprehensivBind/comprehensivBind'
						        })
						      }, 1000);
						    }
						});
					}else if(this.data.isPayPassword==1){
						
						this.showInputLayer()
					}
				}

			}
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})

	},
ticketsCancel(){
	this.setData({
		tickets: true,
    payIs:false,//显示
	})
},
registerFormSubmit(e){
  console.log("===================>",e)
	var formid = e.detail.formId;
	// console.log(e,formid)
	this.payment(formid)
}
})
